﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Common;
/*
 * 1.使用UIManager管理三种相机模式,以达到不同的渲染效果.
 * 2.管理UI的层次,提供接口管理.
 * 3.暂时只启用screenspace_camera,其他置为无效
 
 
 */
public class UIManager : Singleton<UIManager>
{
    public const string PerfabPath = "default/UIRoot";
    GameObject m_root = null;
    Camera camera_overlay = null;
    Camera camera_camera = null;
    Camera camera_3d = null;
    Canvas canvas_overlay = null;
    Canvas canvas_camera = null;
    Canvas canvas_3d = null;


    //screenspace UI序列
    Dictionary<string, QuickList<GameObject>> camera_chain;
    //overlay UI 序列
    Dictionary<string, QuickList<GameObject>> overlay_chain;
    //3d UI 序列
    Dictionary<string, QuickList<GameObject>> three3D_chain;


    public override void Initialize()
    {
        var perfab = Resources.Load(PerfabPath) as GameObject;
        if (perfab != null)
        {
            var root = GameObject.Instantiate(perfab) as GameObject;
            if (root != null)
            {
                root.name = "UIRoot";
                m_root = root;
                Transform r_trans = m_root.transform;
                camera_overlay = r_trans.FindChild("Camera_overlay").GetComponent<Camera>();
                camera_camera = r_trans.FindChild("Camera_camera").GetComponent<Camera>();
                camera_3d = r_trans.FindChild("Camera_3d").GetComponent<Camera>();

                canvas_overlay = r_trans.FindChild("Canvas_overlay").GetComponent<Canvas>();
                canvas_camera = r_trans.FindChild("Canvas_camera").GetComponent<Canvas>();
                canvas_3d = r_trans.FindChild("Canvas_3d").GetComponent<Canvas>();

                if (camera_overlay != null)
                {
                    camera_overlay.enabled = false;
                }
                if (camera_camera != null)
                {
                    camera_camera.enabled = false;
                }
                if (camera_3d != null)
                {
                    camera_3d.enabled = false;
                }
                if (canvas_overlay != null)
                {
                    canvas_overlay.enabled = false;
                }
                if (canvas_camera != null)
                {
                    canvas_camera.enabled = false;
                }
                if (canvas_3d != null)
                {
                    canvas_3d.enabled = false;
                }

                camera_chain = new Dictionary<string, QuickList<GameObject>>();
                overlay_chain = new Dictionary<string, QuickList<GameObject>>();
                three3D_chain = new Dictionary<string, QuickList<GameObject>>();
            }
        }
    }
    public override void Uninitialize()
    {
        if (m_root != null)
        {
            GameObject.Destroy(m_root);
        }
        m_root = null;
        camera_overlay = null;
        camera_camera = null;
        camera_3d = null;
        canvas_overlay = null;
        canvas_camera = null;
        canvas_3d = null;
    }

    //向链中加入UI
    //prefabPath : perfab path
    //nodeName : the name you add;
    // isCreateNew : unuseful;
    //cameraType : UI camera mode;
    //addType : UIAddType.addNewNode:  add one new node; UIAddType.addCurNode:add node to "addNodeName";
    //addNodeName = "addNodeName";
    public bool OpenUIScene(string prefabPath, string nodeName, bool isCreateNew, UICameraType cameraType, UIAddType addType, string addNodeName)
    {
        switch (cameraType)
        {
            case UICameraType.cOverlay:
                return this.OpenUIScene(PerfabPath, nodeName, isCreateNew, overlay_chain,canvas_overlay, addType, addNodeName);
              //  break;
            case UICameraType.cCamera:
                camera_camera.enabled = true;
                return this.OpenUIScene(PerfabPath, nodeName, isCreateNew, camera_chain,canvas_camera, addType, addNodeName);
              //  break;
            case UICameraType.cThreeD:
                return this.OpenUIScene(PerfabPath, nodeName, isCreateNew, three3D_chain,canvas_3d, addType, addNodeName);
              //  break;
            default:
                break;
        }
        return true;
    }
    private bool OpenUIScene(string prefabPath, string nodeName, bool isCreateNew, Dictionary<string, QuickList<GameObject>> UIChain,Canvas cvs, UIAddType addType, string addNodeName)
    {
        if (prefabPath == null)
        {
            Debug.Log("prefab path is null !");
            return false;
        }
        if (nodeName == null)
        {
            Debug.Log("nodeName is null !");
            return false;
        }
        if (UIChain == null)
        {
            Debug.Log("Chain is null, now not surport threeD and overlay");
            return false;
        }
        if (addNodeName == null)
        {
            addType = UIAddType.addNewNode;
        }

        if (addType == UIAddType.addCurNode && addNodeName == null)
        {
            Debug.Log("you must specify one addNodeName");
            return false;
        }

        var prefab = Resources.Load(prefabPath) as GameObject;
        if (prefab != null)
        {
           
            if (addType == UIAddType.addNewNode)//add new node
            {
               /* var node = cvs.transform.FindChild(nodeName).gameObject;
                if ( node == null)
                {   
                    node = GameObject.Instantiate(prefab) as GameObject;
                    node.transform.parent = cvs.transform;
                    node.name = nodeName;
                    node.transform.SetAsLastSibling();
                }
                if (UIChain[nodeName] == null)
                {
                    UIChain[nodeName] = new QuickList<GameObject>();
                }
                UIChain[nodeName].Add(node);*/
                var curNode = cvs.transform.FindChild(nodeName).gameObject;
                if (curNode == null)
	            {
		            curNode = new GameObject();
                    curNode.transform.parent = cvs.transform;
                    curNode.name = nodeName;
                    curNode.transform.SetAsLastSibling();
	            }
                if (UIChain[nodeName] == null)
                {
                    UIChain[nodeName] = new QuickList<GameObject>();
                }
                var node = curNode.transform.FindChild(nodeName).gameObject;
                if (node == null)
                {
                    node = GameObject.Instantiate(prefab) as GameObject;
                    node.transform.parent = curNode.transform;
                    node.name = nodeName;
                    UIChain[nodeName].Add(node);
                }
                node.transform.SetAsLastSibling();
                

            }
            else if (addType == UIAddType.addCurNode)// add to cur node
            {
                var curNode = cvs.transform.FindChild(addNodeName).gameObject;
                if (curNode == null)
                {
                    Debug.Log("this is no node named:" + addNodeName);
                    return false;
                }
                var node = curNode.transform.FindChild(nodeName).gameObject;
                if (node == null)
                {
                    node = GameObject.Instantiate(prefab) as GameObject;
                    node.transform.parent = curNode.transform;
                    node.name = nodeName;
                    UIChain[addNodeName].Add(node);
                }
                node.transform.SetAsLastSibling();
                
            }
        }
        else
        {
            Debug.Log("load source error! please click your prefab path.");
            return false;
        }

        return true;
    }


    public void DestroyNode(string chainName,string nodeName,UICameraType cameraType,bool is_delete)
    {
        switch (cameraType)
        {
            case UICameraType.cOverlay:
                this.DestroyNode(chainName,nodeName,canvas_overlay,is_delete);
                break;
            case UICameraType.cCamera:
                this.DestroyNode(chainName, nodeName, canvas_camera, is_delete);
                break;
            case UICameraType.cThreeD:
                this.DestroyNode(chainName, nodeName, canvas_3d, is_delete);
                break;
            default:
                break;
        }
    }

    private void DestroyNode(string chainName, string nodeName, Canvas canvas, bool is_delete)
    {
        if (canvas == null)
        {
            return;
        }
        var chainNode = canvas.transform.FindChild(chainName).gameObject;
        if (chainNode != null)
        {
            if(nodeName == null)
            {
                if (is_delete == true)
                {
                    GameObject.Destroy(chainNode);
                }
                else
                {
                    chainNode.SetActive(false);
                }
            }
            else
            {
                var node = chainNode.transform.FindChild(nodeName).gameObject;
                if(node != null)
                {
                    if (is_delete == true)
                    {
                        GameObject.Destroy(node);
                    }
                    else
                    {
                        node.SetActive(false);
                    }
                }
            }
            
        }
    }

    public void ResumeNode(string chainName, string nodeName, UICameraType cameraType)
    {
        switch (cameraType)
        {
            case UICameraType.cOverlay:
                this.ResumeNode(chainName, nodeName, canvas_overlay);
                break;
            case UICameraType.cCamera:
                this.ResumeNode(chainName, nodeName, canvas_camera);
                break;
            case UICameraType.cThreeD:
                this.ResumeNode(chainName, nodeName, canvas_3d);
                break;
            default:
                break;
        }
    }

    private void ResumeNode(string chainName, string nodeName, Canvas canvas)
    {
        if (canvas == null)
        {
            return;
        }
        var chainNode = canvas.transform.FindChild(chainName).gameObject;
        if (chainNode != null)
        {
            if (nodeName == null)
            {
                chainNode.SetActive(true);
            }
            else
            {
                var node = chainNode.transform.FindChild(nodeName).gameObject;
                if (node != null)
                {
                    node.SetActive(true);
                }
            }

        }
    }
}
